package com.bit.blog.common.advice;

import com.bit.blog.common.exception.BlogException;
import com.bit.blog.pojo.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ResponseBody
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    public Result exceptionHandler(Exception e){
        log.error("发生异常，e:", e);
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(BlogException e) {
        log.error("发生异常，e:", e);
        return Result.fail(e.getErrMsg());
    }
    @ExceptionHandler
    public Result exceptionHandler(MethodArgumentNotValidException e){
        log.error("发生异常，e:", e);
        String defaultMessage = e.getBindingResult().getFieldError().getDefaultMessage();
        return Result.fail(defaultMessage);
    }
}
